package org.jeecgframework.web.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.UploadHallService;
import org.jeecgframework.web.system.pojo.base.ZyCinemaEntity;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.pojo.base.ZyRoomEntity;
import org.jeecgframework.web.system.pojo.base.ZyUpRoomThread;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**   
 * @Title: UploadHallServiceImpl
 * @Description: 上传影院信息
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Service("uploadHallService")
@Transactional
public class UploadHallServiceImpl extends CommonServiceImpl implements
		UploadHallService {

	/**
	 * @Description 解析EXCEL
	 * @param HttpServletRequest 通过前台请求获得文件流
	 */
	public void parseExcel(HttpServletRequest req,AjaxJson j) {
		FileBean fileBean = UploadFileUtil.UploadFile(req);
		try {
			ZyFileLogEntity fileLog  = new ZyFileLogEntity();
			fileLog.setBusinessType(StaticValue.IMPORT_FILE_HALL_BASE_DATA); //文件业务
			fileLog.setOldFileName(fileBean.getFileRealName());//文件原名
			fileLog.setNewFileName(fileBean.getFileName());//文件新名
			fileLog.setFilePath(fileBean.getSavePath());
			fileLog.setFileType("xlsx");
			fileLog.setStatus("0");
			super.save(fileLog);
			
			j.setObj(fileLog.getFilePath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description 将通过POI解析出来的EXCEL转换成对应的实体LIST
	 * @param XSSFWorkbook Excel2007+对象
	 */
	public void readExcel(String filePath) {
		List<Map<String, Object>> mapList = ExcelUtil.analyzeExcel(filePath, "1");
		List<ZyRoomEntity> zyRoomList = new ArrayList<ZyRoomEntity>();
		List<ZyCinemaEntity> cinemaList = getList(ZyCinemaEntity.class);
		
		for (int i = 1; i < mapList.size(); i++) {
			Map<String, Object> map = mapList.get(i);
			ZyRoomEntity zyRoomEntity = new ZyRoomEntity();
			ZyCinemaEntity zyCinemaEntity = null;
			for (ZyCinemaEntity cinema : cinemaList) {
				if(cinema.getName() != null &&
						cinema.getName().equals(map.get("0").toString())){
					zyCinemaEntity = cinema;
					break;
				}
			}
			zyRoomEntity.setRoomCinema(zyCinemaEntity);
			zyRoomEntity.setRoomNum(StringUtil.getString(map.get("1")));
			String seatNum = StringUtil.getString(map.get("2"));
			zyRoomEntity.setSeatNum(Integer.valueOf(StringUtil.isNotEmpty(seatNum) ? seatNum : "0"));
			String screenType = "";
			switch(StringUtil.getString(map.get("3"))){
				case "等高白幕" : 
					screenType = StaticValue.DIC_SCREEN_TYPE_HIGHWHITE;
					break;
				case "等宽白幕" : 
					screenType = StaticValue.DIC_SCREEN_TYPE_WIDTHWHITE;
					break;
				case "等宽金属幕" : 
					screenType = StaticValue.DIC_SCREEN_TYPE_WIDTHMETAL;
					break;
				case "等高金属幕" : 
					screenType = StaticValue.DIC_SCREEN_TYPE_HIGHMETAL;
					break;
				case "数字幕" : 
					screenType = StaticValue.DIC_SCREEN_TYPE_DIGITAL;
					break;
				default:
					screenType = "";
			}
			zyRoomEntity.setScreenType(screenType);
			zyRoomEntity.setScreenPlus(StringUtil.getString(map.get("4")));
			zyRoomEntity.setScreenWidth(StringUtil.getString(map.get("5")));
			zyRoomEntity.setScreenHight(StringUtil.getString(map.get("6")));	
			zyRoomEntity.setCurtainWidth(StringUtil.getString(map.get("7")));
			zyRoomEntity.setCurtainHight(StringUtil.getString(map.get("8")));
			zyRoomEntity.setShowKm(StringUtil.getString(map.get("9")));
			zyRoomEntity.setScreenLensRatio(StringUtil.getString(map.get("10")));
			zyRoomEntity.setCurtainLensRatio(StringUtil.getString(map.get("11")));
			zyRoomEntity.setRoomSpecialCase(StringUtil.getString(map.get("12")));
			zyRoomEntity.setIsNormal(StringUtil.getString(map.get("13")).trim().toUpperCase().equals("Y")?"1":"0");
			zyRoomEntity.setExceptionReason(StringUtil.getString(map.get("14")));
			zyRoomEntity.setIsDelete("0");
			zyRoomEntity.setCreateBy("");
			zyRoomEntity.setUpdateBy("");
			zyRoomList.add(zyRoomEntity);
		}
		super.batchSave(zyRoomList);
	}
	
	/*public void readExcel(String filePath) {
		try {
			if(StringUtil.isNotEmpty(filePath)){
				File excelFile = new File(filePath);
				XSSFWorkbook xssfWb = ExcelUtil.analyzeExcel2(new FileInputStream(excelFile));
				XSSFSheet sheet = xssfWb.getSheetAt(0);
				int maxUnit = 3000;//每N 条 启动一个线程
				int countRow = sheet.getPhysicalNumberOfRows();
				int threadC = countRow / maxUnit;
				int points = countRow % maxUnit;
				if(threadC > 0){
					if(points > 0){
						threadC = threadC + 1;
					}
				}else{
					threadC = 1;
				}
				System.out.println("启动线程数量:" + threadC);
				System.out.println("共:" + countRow + "条");
				List<ZyCinemaEntity> cinemaList = getList(ZyCinemaEntity.class);
				for (int i = 0; i < threadC; i++) {
					int beginR = i == 0 ? 1 : i * maxUnit + 1;
					int endR = i == (threadC -1 ) ? countRow : beginR + maxUnit - 1 ;
					
					ZyUpRoomThread upRoomThread = new ZyUpRoomThread(beginR,endR,sheet,cinemaList);
					upRoomThread.start();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}*/
	

}
